// pages/home/myApproval/myApproval.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noneTabTitle: ['全部', '发票', '到款', '发票作废', '认领作废', '汇票付款'],
    searchValue: '',
    activeTab: 0,
    secondNoneTab: 0,
    secondTab: 0,
    secondDeptTab: 0,
    tips: '暂无数据',
    listData: [], // 已审批,未审批
    currentPage: 1, // 当前页码
    pageSize: 10, // 每页显示数据
    loading: true,
    remind: '下拉加载更多',
    nodata: false, //是否没有数据
    result: [], // 复选
    allChecked: false, // 全选
    isBatch: false, // 是否批量
  },
  request() {
    wx.navigateTo({
      url: '../invoiceApplication/invoiceApplication',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  getData() {
    this.data.result = []

    if (this.data.activeTab == 0) {
      this.getNotYet()
    } else if (this.data.activeTab == 1) {
      this.getApproved()
    } else {
      this.getDeptApproved()
    }
  },
  getNotYet() {
    let that = this;
    let param = {
      searchStr: that.data.searchValue ? that.data.searchValue : '',
      pageNum: that.data.currentPage,
      pageSize: that.data.pageSize,
      type: that.data.secondNoneTab == 0 ? 3 : that.data.secondNoneTab > 2 ? that.data.secondNoneTab + 1 : that.data.secondNoneTab
    };
    api.getMyExamine({
      data: param,
      success: function (res) {
        console.log(res.data.data)
        that.analysis(res.data)
      }
    })
  },
  getApproved() {
    let that = this;
    let claimedType = 2;
    if (that.data.secondTab == 0) {
      claimedType = 2
    } else if (that.data.secondTab == 1) {
      claimedType = 3
    } else if (that.data.secondTab == 2) {
      claimedType = 4
    }
    let param = {
      flowStatus: claimedType,
      searchStr: that.data.searchValue ? that.data.searchValue : '',
      pageNum: that.data.currentPage,
      pageSize: that.data.pageSize
    };
    api.getMyExamineHistory({
      data: param,
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data)
          that.analysis(res.data)
        }

      }
    })
  },
  getDeptApproved() {
    let that = this;
    let claimedType = 2;
    if (that.data.secondDeptTab == 0) {
      claimedType = 2
    } else if (that.data.secondDeptTab == 1) {
      claimedType = 3
    } else if (that.data.secondDeptTab == 2) {
      claimedType = 4
    }
    let param = {
      flowStatus: claimedType,
      searchStr: that.data.searchValue ? that.data.searchValue : '',
      pageNum: that.data.currentPage,
      pageSize: that.data.pageSize
    };
    api.getMyDeptExamineHistory({
      data: param,
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data)
          that.analysis(res.data)
        }

      }
    })
  },
  // 批量审批
  goBatch() {
    this.setData({
      isBatch: !this.data.isBatch
    })
  },
  onCheckChange(event) {
    if (event.detail.length == this.data.listData.length) {
      this.data.allChecked = true
    } else {
      this.data.allChecked = false
    }
    this.setData({
      result: event.detail,
      allChecked: this.data.allChecked
    });
    console.log(this.data.result)
  },
  onAllChange(event) {
    console.log(event)
    this.data.result = [];
    if (event.detail) {
      for (let i = 0; i < this.data.listData.length; i++) {
        this.data.result.push(this.data.listData[i].id + '')
      }
    }
    console.log(this.data.result)
    this.setData({
      allChecked: event.detail,
      result: this.data.result
    });
  },
  goDetail(e) {
    // if (!e.currentTarget.dataset.isexaminer) {
    //   wx.showToast({
    //     icon: 'error',
    //     title: '没有权限查看',
    //   })
    //   return;
    // }
    if (e.currentTarget.dataset.pagejumptype === '到款认领') {
      if (e.currentTarget.dataset.editable) {
        if (e.currentTarget.dataset.status == 1) {
          wx.navigateTo({
            url: '/pages/receive/claim/claim?claimId=' + e.currentTarget.dataset.id,
          })
          return;
        }
        if (e.currentTarget.dataset.status == 3) {
          wx.navigateTo({
            url: '/pages/receive/returnedClaim/returnedClaim?claimId=' + e.currentTarget.dataset.id,
          })
          return;
        }
      }
      wx.navigateTo({
        url: '/pages/receive/details/details?claimId=' + e.currentTarget.dataset.id,
      })
    }
    if (e.currentTarget.dataset.pagejumptype === '到款认领作废') {
      wx.navigateTo({
        url: '/pages/receive/invalidDetails/invalidDetails?abolishId=' + e.currentTarget.dataset.id,
      })
    }
    if (e.currentTarget.dataset.pagejumptype === '发票作废') {
      wx.navigateTo({
        url: '/pages/voidDetails/voidDetails?id=' + e.currentTarget.dataset.id,
      })
    }
    if (e.currentTarget.dataset.pagejumptype === '发票') {
      wx.navigateTo({
        url: '/pages/invoicedetails/invoicedetails?id=' + e.currentTarget.dataset.id,
      })
    }
    if (e.currentTarget.dataset.pagejumptype === '汇票付款') {
      wx.navigateTo({
        url: '/pages/draftFkDetails/draftFkDetails?id=' + e.currentTarget.dataset.id,
      })
    }
    // if (e.currentTarget.dataset.isinvoice) {
    //   if (e.currentTarget.dataset.isabolish) {
    //     wx.navigateTo({
    //       url: '/pages/voidDetails/voidDetails?id=' + e.currentTarget.dataset.id,
    //     })
    //   } else {
    //     wx.navigateTo({
    //       url: '/pages/invoicedetails/invoicedetails?id=' + e.currentTarget.dataset.id,
    //     })
    //   }
    // } else {
    //   if (e.currentTarget.dataset.isabolish) {
    //     wx.navigateTo({
    //       url: '/pages/receive/invalidDetails/invalidDetails?abolishId=' + e.currentTarget.dataset.id,
    //     })
    //   } else {
    //     if (e.currentTarget.dataset.editable) {
    //       if (e.currentTarget.dataset.status == 1) {
    //         wx.navigateTo({
    //           url: '/pages/receive/claim/claim?claimId=' + e.currentTarget.dataset.id,
    //         })
    //         return;
    //       }
    //       if (e.currentTarget.dataset.status == 3) {
    //         wx.navigateTo({
    //           url: '/pages/receive/returnedClaim/returnedClaim?claimId=' + e.currentTarget.dataset.id,
    //         })
    //         return;
    //       }
    //     }
    //     wx.navigateTo({
    //       url: '/pages/receive/details/details?claimId=' + e.currentTarget.dataset.id,
    //     })
    //   }

    // }

  },
  // 批量同意
  onAgree() {
    let that = this;
    api.batchApprove({
      data: {
        flowIdStr: that.data.result.join(','),
        flowNotes: '',
        type: that.data.secondNoneTab == 0 ? 3 : that.data.secondNoneTab > 2 ? that.data.secondNoneTab + 1 : that.data.secondNoneTab,
        isAll: that.data.allChecked ? '1' : '',
        searchStr: that.data.searchValue ? that.data.searchValue : '',
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: '审批成功',
          })
          that.clearData()
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
        that.setData({
          isBatch: false
        })
      }
    })
  },
  // 批量拒绝
  onDecline() {
    let that = this;
    api.batchDecline({
      data: {
        flowIdStr: that.data.result.join(','),
        flowNotes: '',
        type: that.data.secondNoneTab == 0 ? 3 : that.data.secondNoneTab > 2 ? that.data.secondNoneTab + 1 : that.data.secondNoneTab,
        isAll: that.data.allChecked ? '1' : '',
        searchStr: that.data.searchValue ? that.data.searchValue : '',
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: '审批成功',
          })
          that.clearData()
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
        that.setData({
          isBatch: false
        })
      }
    })
  },
  // 处理数据
  analysis(data) {
    let that = this;
    if (data.code == 200) {
      if (data.data.list.length > 0) {
        for (let i = 0; i < data.data.list.length; i++) {
          let now = new Date(data.data.list[i].flowTime);
          data.data.list[i].flowTime = util.formatTime(now).split(' ')[0];
          data.data.list[i].flowMoney = util.formatFigure(data.data.list[i].flowMoney)
        }
        that.data.listData = that.data.listData.concat(data.data.list);
        for (let i = 0; i < that.data.listData.length; i++) {
          if (that.data.allChecked && that.data.activeTab == 0) {
            that.data.result.push(that.data.listData[i].id + '')
          }
        }
        that.data.totalPage = data.data.totalPage
        that.setData({
          listData: that.data.listData,
          result: that.data.result,
          loading: false,
          nodata: false,
          remind: data.data.totalPage > data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
        })
      } else {
        that.setData({
          nodata: true
        })
      }
    }
  },
  // 清空数据并重新加载数据
  clearData() {
    this.setData({
      listData: [],
      currentPage: 1,
      loading: true
    })
    this.getData()
  },
  // 搜索
  onSearch(event) {
    this.setData({
      searchValue: event.detail.value ? event.detail.value : event.detail,
    })
    this.clearData()
  },
  onChange(event) {
    this.setData({
      activeTab: event.detail.index,
      secondTab: 0,
      secondDeptTab: 0,
      secondNoneTab: 0,
      searchValue: '',
      isBatch: false,
      result: [],
      allChecked: false
    })
    this.clearData()
  },
  secondNoneChange(event) {
    this.setData({
      searchValue: '',
      secondNoneTab: event.detail.index,
      isBatch: false,
      result: [],
      allChecked: false
    })
    this.clearData()
  },
  secondDeptChange(event) {
    this.setData({
      searchValue: '',
      secondDeptTab: event.detail.index
    })
    this.clearData()
  },
  secondChange(event) {
    this.setData({
      searchValue: '',
      secondTab: event.detail.index
    })
    this.clearData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clearData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage > this.data.currentPage) {
      ++this.data.currentPage;
      this.setData({
        loading: true
      })
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})